package com.zzyl.serve.service.impl;


import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zzyl.serve.mapper.NursingPlanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.service.INursingLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理等级Service业务层处理
 *
 * @author zzyl
 * @date 2025-10-13
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService {
    //缓存key,方便调用
    private final String NURSING_LEVEL_ALL = "nursing_level:all";
    //redis需要缓存同步：增删改
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Integer id) {
        return this.getById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel) {
        List<NursingLevel> levels = nursingLevelMapper.selectNursingLevelList(nursingLevel);
        if (CollectionUtils.isNotEmpty(levels)){
            //封装数据
            levels.forEach(level -> {
                level.setPlanName(nursingPlanMapper.selectNursingPlanById(level.getLplanId()).getPlanName());
            });
        }
        return levels;
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel) {
        boolean save = this.save(nursingLevel);
        //删除缓存
        deleteRedisCache();

        return save ? 1 : 0;
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel) {
        boolean b = this.updateById(nursingLevel);
        deleteRedisCache();
        return b ? 1 : 0;
    }


    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Integer id) {
        boolean b = this.removeById(id);
        deleteRedisCache();
        return b ? 1 : 0;
    }

    /**
     * 查询所有启用护理等级
     * @return
     */
    @Override
    public List<NursingLevel> listAll() {
        //先用redis查询
        List<NursingLevel> level = (List<NursingLevel>) redisTemplate.opsForValue().get(NURSING_LEVEL_ALL);
        //命中就返回，为命中就查询数据库
        if (CollectionUtils.isNotEmpty(level)) {
            return level;
        }
        //sql查询
        level = nursingLevelMapper.listAll();
        //数据存储到redis
        redisTemplate.opsForValue().set(NURSING_LEVEL_ALL, level);
        return level;
    }

    /**
     * 删除缓存的方法，可以直接调用，不需要参数
     */
    private void deleteRedisCache() {
        // 删除缓存
        redisTemplate.delete(NURSING_LEVEL_ALL);
    }
}
